#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/icon/images/chart.ico"));
    setWindowState(Qt::WindowMaximized);
    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowSystemMenuHint);
    m_areaMovable = geometry();
    m_bPressed = false;
    initUI();

    /****************************窗口关闭有问题，会导致内存泄露，暂时先用着*********************************************/
    connect(ui->headerBar->closeButton,&QPushButton::clicked,[=](){

        this->close();

    });


}

Widget::~Widget()
{
    delete ui;
}



void Widget::initUI()
{
    ui->toolButton_index->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    ui->toolButton_watch->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    ui->toolButton_set->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    ui->toolButton_about->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    //初始化功能界面
    index=new Index(this);
    set=new Set(this);
    about=new About(this);
    watch=new Watch(this);


    ui->stackedWidget->addWidget(index);
    ui->stackedWidget->addWidget(watch);
    ui->stackedWidget->addWidget(set);
    ui->stackedWidget->addWidget(about);


    //初始化按钮组，组合侧边栏按钮
    m_button_group=new QButtonGroup(this);
    m_button_group->addButton(ui->toolButton_index,0);
    m_button_group->addButton(ui->toolButton_watch,1);
    m_button_group->addButton(ui->toolButton_set,2);
    m_button_group->addButton(ui->toolButton_about,3);
    //构建跳转信号槽
    connect(m_button_group, static_cast<void (QButtonGroup::*)(int)
            >(&QButtonGroup::buttonClicked),
            ui->stackedWidget, &QStackedWidget::setCurrentIndex);

    //设置默认选中得页面
    ui->stackedWidget->setCurrentIndex(0);



}

void Widget::setAreaMovable(const QRect rt)
{
    if(m_areaMovable != rt)
    {
        m_areaMovable = rt;
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    //鼠标左键
    if(event->button() == Qt::LeftButton)
    {
        m_ptPress = event->pos();
        qDebug() << pos() << event->pos() << m_ptPress;
        m_bPressed = m_areaMovable.contains(m_ptPress);
    }
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bPressed)
    {
        qDebug() << pos() << event->pos() << m_ptPress;
        move(pos() + event->pos() - m_ptPress);
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    m_bPressed = false;
}

